package com.dhecp.project.hr.service.impl;

import com.alibaba.fastjson.JSON;
import com.dhecp.common.core.domain.AjaxResult;
import com.dhecp.common.utils.MessageUtils;
import com.dhecp.common.utils.RandomValueUtils;
import com.dhecp.core.constant.DataFiltering;
import com.dhecp.core.utils.PageUtils;
import com.dhecp.framework.util.ShiroUtils;
import com.dhecp.project.common.service.UpdateLogService;
import com.dhecp.project.common.service.impl.BaseModuleServiceImpl;
import com.dhecp.project.hr.domain.AmazonShopExchangeRate;
import com.dhecp.project.hr.mapper.AmazonShopExchangeRateMapper;
import com.dhecp.project.hr.service.AmazonShopExchangeRateService;
import com.dhecp.project.system.service.SysDictionaryForFieldService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @program: kjecp
 * @description: 店铺订单
 * @author: hlf
 * @create: 2022-10-09 14:36
 */
@Service
public class AmazonShopExchangeRateServiceImpl extends BaseModuleServiceImpl implements AmazonShopExchangeRateService {

    @Autowired
    private AmazonShopExchangeRateMapper businessMapper;

    @Autowired
    private UpdateLogService updateLogService;
    @Autowired
    private SysDictionaryForFieldService dictionaryForFieldService;


    /**
     * 查询
     * @param params
     * @return List - AttendanceQJ
     * @throws Exception
     * @author DH-Xxj
     */
    @Override
    public List<AmazonShopExchangeRate> queryList(Map<String, Object> params) throws Exception {
        params.put(DataFiltering.TABLE_ALIAS, "t1");	//查询表别名
        params.put(DataFiltering.DATA_WHERE, dictionaryForFieldService.getWhereStr(params));	//常规查询条件SQL语句
        params.put(DataFiltering.DATA_SCODE, dictionaryForFieldService.getDataScode(params));	//数据权限查询条件SQL语句
        if (params.get("field") == null || params.get("field") == ""){
            String orderBy = "code asc";
            PageUtils.startPageOrder(orderBy);
        }else{
            PageUtils.startPage();
        }
        return businessMapper.select(params);
    }

    /**
     * @Description: 查询指定单nid的首记录
     * @Param: [params]
     * @return: com.dhecp.project.storage.domain.AmazonShopExchangeRate
     * @Author: hlf
     * @date: 2022-05-09 10:10
     */
    @Override
    public AmazonShopExchangeRate findByGuid(Map<String, Object> params) throws Exception {
        params.put(DataFiltering.TABLE_ALIAS, "t1");    //查询表别名
        params.put(DataFiltering.DATA_SCODE, dictionaryForFieldService.getDataScode(params));    //数据权限查询条件SQL语句
        return businessMapper.findById(params);
    }

    /**
     * 保存
     * @param moduleTable 必要参数：功能唯一标识
     * @param infoMap 必要参数：非网格字段（包括头部、底部所有显示和隐藏的字段）
     * @param infoItems 必要参数：对应实体类的json格式字符串
     * @return AjaxResult
     * @author DH-Xxj
     */
    @SuppressWarnings("unchecked")
    @Override
    public AjaxResult saveData(Map<String, Object> params) {
//        AmazonShopExchangeRate oldData = new AmazonShopExchangeRate();
        AmazonShopExchangeRate newData = JSON.parseObject(params.get("infoItems").toString(), AmazonShopExchangeRate.class);

//        Map<String, Object> params2 = new HashMap<String, Object>();
//        params2.put("guid",newData.getGuid());
//        oldData = businessMapper.findById(params2);

        if (newData.getGuid() != null && !newData.getGuid().equals("")){
            businessMapper.update(newData);
//            updateLogService.addUpdateLog(params.get("moduleTable").toString(),newData.getGuid(),oldData,newData,"修改");
        }else{
            newData.setGuid(RandomValueUtils.getGUID());
            businessMapper.insert(newData);
//            updateLogService.addInsertLog(params.get("moduleTable").toString(),newData.getGuid(),"新增");
        }

        return AjaxResult.success(MessageUtils.message("module.success.issuccess"), newData.getGuid());
    }

    /**
     * 删除
     * @param params
     * @return AjaxResult
     * @author DH-Xxj
     */
    @Override
    public AjaxResult deleteData(Map<String, Object> params) {
        List<AmazonShopExchangeRate> businessDataList = JSON.parseArray(params.get("infoItems").toString(), AmazonShopExchangeRate.class);
        if (businessDataList == null || businessDataList.size() == 0) {
            return AjaxResult.error(MessageUtils.message("module.error.jsontoentyerror"));
        }
        for (AmazonShopExchangeRate businessData : businessDataList) {
            businessMapper.delete(businessData);
        }
        return AjaxResult.success(MessageUtils.message("module.success.issuccess"));
    }
}